//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Blogs.Controls
{
    /// <summary>
    /// Summary description for BlogRatePost.
    /// </summary>
    public class BlogRatePost : CommunityServer.Controls.RatingControl
    {
		CSContext csContext = CSContext.Current;
		public event EventHandler RatingsChanged;

		#region Public Properties

        private WeblogPost _p;
        public WeblogPost Post
        {
            get
            {
                if(_p == null)
                {
                    Weblog wl = Weblogs.GetWeblog(csContext.ApplicationKey);
                    _p = WeblogPosts.GetWeblogEntry(wl.SectionID,csContext.PostID);
                }
                return _p;
            }
            set
            {
                _p = value;
            }
        }

		#endregion

		#region RatingControl Implementation

		private double _currentRating = -1;
		public override double CurrentRating
		{
			get
			{
				if (_currentRating == -1)
					_currentRating = this.Post.RatingAverage;

				return _currentRating;
			}
			set
			{
				_currentRating = value;
			}
		}

		private int _currentVotes = -1;
		public override int CurrentVotes
		{
			get
			{
				if (_currentVotes == -1)
					_currentVotes = this.Post.TotalRatings;

				return _currentVotes;
			}
			set
			{
				_currentVotes = value;
			}
		}

		private string _viewDetailsScript = String.Empty;
		public override string ViewDetailsScript
		{
			get
			{
				return _viewDetailsScript;
			}
			set
			{
				_viewDetailsScript = value;
			}
		}

        private bool _userCanRate = true;
        private bool _calculated = false;

		public override bool UserCanRate 
		{ 
			get
			{
                if(!_calculated)
                {
                    if(csContext.User.IsAnonymous)
                        _userCanRate = false;

                    if(_userCanRate)
                    {
                        WeblogPermission permission = Post.Section.ResolvePermission( csContext.User ) as WeblogPermission;
                        if(!permission.Reply)
                            _userCanRate = false;
                    }

                     _calculated = true;
                }

                return _userCanRate;
			}
		}

		public override void Rate(double value)
		{
			int currentRating = Ratings.GetRating(RatingType.Thread, this.Post.ThreadID, csContext.User.UserID);
			Rating rating = new Rating(RatingType.Thread, this.Post.ThreadID, csContext.User, (int) value);
			Ratings.Rate(rating);

			if (currentRating != -1)
			{
				_currentVotes = this.Post.TotalRatings + 1;
				_currentRating = (this.Post.RatingSum + value) / (this.Post.TotalRatings + 1);
			}
			else
				_currentRating = ((this.Post.RatingSum - currentRating) + value) / this.Post.TotalRatings;

			OnRatingsChange(new EventArgs());
		}

		#endregion

		protected void OnRatingsChange(EventArgs e)
		{
			if(RatingsChanged != null)
				RatingsChanged(this,e);
		}
    }
}
